using System;
using System.Collections.Generic;
using DDA;
using Unity.VisualScripting;
using UnityEditor.Experimental.GraphView;
using UnityEngine;
using UnityEngine.UIElements;
using YDExtendTool.Scripts;
using YDExtendTool;
using Edge = UnityEditor.Experimental.GraphView.Edge;

namespace YDExtendTool.Editor.TreeDataCreator
{
    public class TreeNodeView : Node
    {
        public TreeNodeBase NodeData;

        public Port InputPort = null;
        public Port OutputPort = null;
        public TreeNodeView(TreeNodeBase nodeData)
        {
            NodeData = nodeData;
            title = nodeData.NodeName;
            InitNodeView(nodeData);
            // this.titleContainer.style.backgroundColor
        }

        public void InitNodeView(TreeNodeBase nodeData)
        {
            switch (nodeData)
            {
                case DDATreeNodeBase ddaTreeNodeBase:
                    if (ddaTreeNodeBase is DDAStart)
                    {
                        SetRoot(null);
                    } else{
                        InputPort = CreateInputPort(false);
                    }
                    if (ddaTreeNodeBase.NextNodeMaxNum == 1)
                    {
                        OutputPort = CreateOutPort(true);
                    }
                    else if (ddaTreeNodeBase.NextNodeMaxNum > 1)
                    {
                        OutputPort = CreateOutPort(false);
                    }
                    inputContainer.Add(InputPort);
                    outputContainer.Add(OutputPort);
                    break;
                case TreeNodeMultipleNextBase stSelect:
                    InputPort = CreateInputPort(false);
                    OutputPort = CreateOutPort(false);
                    inputContainer.Add(InputPort);
                    outputContainer.Add(OutputPort);
                    break;
            }

        }
        public override void SetPosition(Rect newPos)
        {
            base.SetPosition(newPos);
            NodeData.Position = new Vector2(newPos.xMin, newPos.yMin);
        }

        public override void BuildContextualMenu(ContextualMenuPopulateEvent evt)
        {
            // base.BuildContextualMenu(evt);
            // evt.menu.AppendAction("Set Root",SetRoot);
            evt.menu.AppendAction("复制",Copy);
            evt.menu.AppendAction("重置连接",Reconnected);
        }
        

        private void SetRoot(DropdownMenuAction obj)
        {
            TreeNodeDataBase data = UnityEngine.Resources.Load<TreeNodeDataBase>(TreeDataSetting.Instance.EditTreeDataPath);
            if (data != null)
            {
                data.RootNode = NodeData;
            }
        }

        private void Copy(DropdownMenuAction obj)
        {
            TreeDataCreator.instance.View.CreateNode(this.NodeData.GetType(),this.NodeData.Position + Vector2.one * 10f);
        }

        private void Reconnected(DropdownMenuAction obj)
        {
            if (NodeData is DDATreeNodeBase node)
            {
                node.OnConnected(null);
            }
        }

        public override void OnSelected()
        {
            base.OnSelected();
            this.title = NodeData.NodeName;
            TreeDataCreator.instance.Inspector.UpdateViewData();
        }

        private static StyleColor activeColor = new StyleColor(new Color(0, 1, 0, 0.5f));
        private static StyleColor disactiveColor = new StyleColor(new Color(0, 0, 0, 0.5f));
        public void FlushView()
        {
            title = NodeData.NodeName;
            if (NodeData is DDATreeNodeBase node)
            {
                switch (node.state)
                {
                    case DDATreeNodeState.Enable:
                        this.titleContainer.style.backgroundColor = activeColor;
                        break;
                    case DDATreeNodeState.Disable:
                        this.titleContainer.style.backgroundColor = disactiveColor;
                        break;
                }
                // node.OnConnected(null);
            }
        }

        public Port CreateInputPort(bool isSingle)
        {
            Port port = Port.Create<Edge>(Orientation.Horizontal, Direction.Input, isSingle? Port.Capacity.Single:Port.Capacity.Multi,
                typeof(TreeNodeView));
            return port;
        }
        
        public Port CreateOutPort(bool isSingle)
        {
            Port port = Port.Create<Edge>(Orientation.Horizontal, Direction.Output,isSingle? Port.Capacity.Single:Port.Capacity.Multi,
                typeof(TreeNodeView));
            return port;
        }

        public static Edge PortLink(Port port1, Port port2)
        {
            var tempEdge = new Edge()
            {
                output = port1,
                input = port2
            };
            tempEdge.input?.Connect(tempEdge);
            tempEdge.output?.Connect(tempEdge);
            return tempEdge;
        }

        public void LinkLine()
        {
            var treeView = TreeDataCreator.instance.View;
            var nodeDict = TreeDataCreator.instance.VisitNodes;
            TreeNodeView nodeView;
            switch (NodeData)
            {
                case TreeNodeMultipleNextBase treeNodeMultipleNextBase:
                    foreach (var node in treeNodeMultipleNextBase.ChildNodes)
                    {
                        if (node!=null && nodeDict.TryGetValue(node.Guid, out nodeView))
                        {
                            treeView.AddElement(PortLink(OutputPort,nodeView.InputPort));
                        }
                    }
                    break;
            }
        }

    }
}
